package com.liyghting.datastructurealgorithm.stack;

/**
 * 基于链表实现栈
 * 
 * @author LiYiGuang
 * @date 2018/11/21 17:17
 */
public class StackBasedLinkedList {
	private Node top = null;
	private int size;

	public void push(String data) {
		if (top == null) {
			top = new Node(data, null);
		} else {
			Node currentNode = new Node(data, top);
			top = currentNode;
		}
		size++;
	}

	public String pop() {
		if (top == null)
			return null;
		String data = top.data;
		top = top.next;
		if (size > 0)
			size--;
		return data;
	}

	public void printAll() {
		Node node = top;
		while (node != null) {
			System.out.print(node.data + " ");
			node = node.next;
		}
		System.out.println();
	}

	public void clear() {
		size = 0;
		top = null;
	}

	public int size() {
		return size;
	}

	class Node {
		private String data;
		private Node next;

		Node(String data, Node next) {
			this.data = data;
			this.next = next;
		}
	}

	public static void main(String[] args) {
		StackBasedLinkedList stack = new StackBasedLinkedList();
		for (int i = 0; i < 20; i++) {
			stack.push(i + "");
		}
		stack.printAll();
		for (int i = 0; i < 15; i++) {
			stack.pop();
		}
		stack.printAll();

	}
}
